package com.zhfi.dataapply.service.impl;

import com.zhfi.dataapply.entity.TzResponse;
import com.zhfi.dataapply.service.ForwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

//@Service
public class FakeForward implements ForwardService {

    @Autowired
    private WebClient webClient;

    @Override
    public TzResponse requestForwarder(String url, Map<String, Object> params) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("url", url);
        map.put("params", params);
        Mono<TzResponse> mono = webClient.post()
                .uri("http://103.210.170.34:8050/api/forward")
                .body(Mono.just(map), Map.class)
                .retrieve()
                .bodyToMono(TzResponse.class);
        TzResponse tzResponse = mono.block();
        System.out.println("-------------------------");
        System.out.println("请求路径" + url);
        System.out.println("请求参数" + params);
        System.out.println(tzResponse);
        if ("004".equals(tzResponse.getResCode())) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return requestForwarder(url, params);
        }
        return tzResponse;
    }
}
